var app = getApp();
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var comment = require('../widget/comment/comment');
var storage = require('../../utils/storage');
var filter = require('../widget/filter');
var formatTime = require('../widget/time');
var commentList = require('../widget/commentList');
var goTop = require('../widget/goTop/goTop');
var timestamp = require('../widget/timestamp');

var mark = 0;
var pageSetting = {
    callback:function(componentName){
        var that = this;
        console.log(componentName)
        switch(componentName){
            case 'commentContent':
                var commentData = that.data['commentData'];
                commentFun(commentData,arguments[1]);
            break;
            case 'likeFun':

                var likeData = arguments[1];
                like(likeData);
                break;
            case 'delcommentFun':

                var delData = arguments[1];
                delcommentFun(delData);
                break;
            case 'removeBindtapFun':

                var removeFlag = arguments[1];
                removeBindtapFun(removeFlag);
                break;
            case 'delStairComment':
                delStairComment(arguments[1]);
                break;
            case 'delSecondComment':
                delSecondComment(arguments[1]);
                break;
            case 'pullUpLoad':
                pullUpLoad();
                break;
            case 'onCommentShow':
                removeBindtapFun({
                    'removebindtap':1
                });
                break;
            default:
                break;
        }

        //评论
        function commentFun(commentData,content){
            var data = {};
            if(commentData['type'] == 1){
                data = {
                    'self:community_api:community:comment':{
                        'articleId':commentData['articleid'],
                        'content':content,
                        'commentType':0,
                        'parentUserId':commentData['parentuserid'],
                        'parentCommentId':commentData['parentcommentid'],
                        'pParentCommentId':commentData['pparentcommentid'],
                        'relateInfo':'',
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk'],
                        'come_from':commentData['comefrom']
                    }
                }
            }else if(commentData['type'] == 2){
                data = {
                    'self:community_api:community:comment':{
                        'articleId':commentData['articleid'],
                        'content':content,
                        'commentType':commentData['commenttype'],
                        'parentUserId':commentData['parentuserid'],
                        'parentCommentId':commentData['parentcommentid'],
                        'pParentCommentId':commentData['pparentcommentid'],
                        'relateInfo':commentData['relateinfo'],
                        'relatePPInfo':commentData['relateppinfo'],
                        'relatePPAccount':commentData['relateppaccount'],
                        'id':commentData['id'],
                        'come_from':commentData['comefrom'],
                        'relatePPAccountType':commentData['relateppaccounttype'],
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk']
                    }
                }
            }else {
                
                data = {
                    'self:community_api:community:comment':{
                        'articleId':commentData['articleid'],
                        'content':content,
                        'commentType':commentData['commenttype'],
                        'parentUserId':commentData['parentuserid'],
                        'parentCommentId':commentData['parentcommentid'],
                        'pParentCommentId':commentData['pparentcommentid'],
                        'relateInfo':commentData['relateinfo'],
                        'relatePPInfo':commentData['relateppinfo'],
                        'relatePPAccount':commentData['relateppaccount'],
                        'id':commentData['id'],
                        'relatePPAccountType':commentData['relateppaccounttype'],
                        'relatePInfo':commentData['relatepinfo'],
                        'relatePAccount':commentData['relatepaccount'],
                        'relatePAccountType':commentData['relatepaccounttype'],
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk'],
                        'come_from':commentData['comefrom']
                    }
                }
            }
            
            // return false;
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                var commentListData = that['data']['commentListData'];
                if( !!res['self:community_api:community:comment']['ret']){
                    // modal.toast(res['self:community_api:community:comment']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }
                
                if(commentData['type'] == 1){
                    
                    that.onLoad({
                        'id':that['data']['commentListData']['article_id']
                    });
                }else if (commentData['type'] == 2){
                    var returnComment = res['self:community_api:community:comment']['data'];                 
                    
                    var newComment = commentList.commentList(returnComment,commentData);
                    if (!commentListData['comment'][commentData.index]['secondCommentInfo']['commentInfo']){
                    
                        commentListData['comment'][commentData.index]['secondCommentInfo']={
                            'commentInfo':[newComment]
                        }
                        commentListData['comment'][commentData.index]['commentCount'] += 1;
                    }else{
                        
                        commentListData['comment'][commentData.index]['secondCommentInfo']['commentInfo'].push(newComment);
                        commentListData['comment'][commentData.index]['commentCount'] += 1;
                    }                    

                    that.setData({
                        commentListData:commentListData
                    });
                }else {
                    var returnComment = res['self:community_api:community:comment']['data'];
                    var newComment = commentList.commentList(returnComment,commentData); 
                    commentListData['comment'][commentData.index]['secondCommentInfo']['commentInfo'].push(newComment);
                    commentListData['comment'][commentData.index]['commentCount'] += 1;
                    that.setData({
                        commentListData:commentListData
                    });
                }
            })
        }

        //点赞功能
        function like(data){
            if (data['liketabflag'] != 0){
                return false;
            }
            var commentListData = that.data['commentListData'];
            commentListData['comment'][data.index]['likeTabFlag'] = 1;
            that.setData({
                    commentListData:commentListData
            });
            var likeData = {};
            if(data['type'] == -1){
                likeData = {
                    'self:community_api:community:like':{
                        'articleId':data['articleid'],
                        'commentId':data['commentid'],
                        'likeType':data['type'],
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk'],
                        'come_from':data['comefrom']
                    }
                }
            }else {
                likeData = {
                    'self:community_api:community:like':{
                        'articleId':data['articleid'],
                        'commentId':data['commentid'],
                        'likeType':data['type'],
                        'relateInfo':data['relateinfo'],
                        'relateAccount':data['relateaccount'],
                        'id':data['id'],
                        'come_from':data['comefrom'],
                        'relateAccountType':data['relateaccounttype'],
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk']
                    }
                }
            }
            
            request.post({
                path: '/wxapp_api/api'
            },likeData)
            .then(function (res){

                if(res['self:community_api:community:like']['ret'] != 0){
                    // modal.toast(res['self:community_api:community:like']['msg']);
                    commentListData['comment'][data.index]['likeTabFlag'] = 0;                    
                    that.setData({
                        commentListData:commentListData
                    });
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }                
                if(data.type != 1){
                    
                    commentListData['comment'][data.index]['myLike'] = 0;
                    commentListData['comment'][data.index]['likeCount'] -= 1;
                    commentListData['comment'][data.index]['likeTabFlag'] = 0;
                }else{
                    
                    commentListData['comment'][data.index]['myLike'] = 1;
                    commentListData['comment'][data.index]['likeCount'] += 1;
                    commentListData['comment'][data.index]['likeTabFlag'] = 0;
                }
                
                that.setData({
                    commentListData:commentListData
                });
            })
        }

        //删除一级评论
        //
        function delStairComment(delData){
            if(mark !=0){
                return false;
            }
            mark = delData['commentid'];
            var data = {
                'self:community_api:community:deleteComment':{
                    'commentId':delData['commentid'],
                    'articleId':0,
                    'timestamp': timestamp['timestamp'],
                    'gtk': timestamp['gtk']
                }
            }
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                if(res['self:community_api:community:deleteComment']['ret'] != 0){
                    // modal.toast(res['self:community_api:community:deleteComment']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }
                var commentListData = that.data['commentListData'];
                commentListData['comment'].splice([delData.index],1);
                commentListData['comment_count'] -= 1;
                that.setData({
                    commentListData:commentListData
                });
                mark = 0;
            })
        }

        // 删除按钮展示
        function delcommentFun(data){
            var commentListData = that['data']['commentListData'];
            for(var i = 0 ;i <commentListData['comment'].length;i++){
                var commentInfo = commentListData['comment'][i]['secondCommentInfo']['commentInfo'];
                if(commentInfo){
                    for(var j=0;j< commentInfo.length;j++){
                        commentInfo[j]['isDel'] = true;
                    }
                }
            }
            commentListData['comment'][data.parentidx]['secondCommentInfo']['commentInfo'][data.itemidx]['isDel'] = false;
            that.setData({
                commentListData:commentListData,
                removeBindtap:1
            });
            
        }

        //删除按钮隐藏
        function removeBindtapFun(removeFlag){
            
            if(removeFlag.removebindtap == 1){
                var commentListData = that['data']['commentListData'];
                for(var i = 0 ;i <commentListData['comment'].length;i++){
                    var commentInfo = commentListData['comment'][i]['secondCommentInfo']['commentInfo'];
                    if(commentInfo){
                        for(var j=0;j< commentInfo.length;j++){
                            commentInfo[j]['isDel'] = true;
                        }
                    }
                }
                that.setData({
                    commentListData:commentListData,
                    removeBindtap:0
                });
            }
        }

        //删除二级评论
        function delSecondComment(delSecondData){
            if(mark !=0){
                return false;
            }
            mark = delSecondData['commentid'];
            var data = {
                'self:community_api:community:deleteComment':{
                    'commentId':delSecondData['commentid'],
                    'articleId':0,
                    'timestamp': timestamp['timestamp'],
                    'gtk': timestamp['gtk']
                }
            };
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                if(res['self:community_api:community:deleteComment']['ret'] != 0){
                    // modal.toast(res['self:community_api:community:deleteComment']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }
                var commentListData = that.data['commentListData'];
                commentListData['comment'][delSecondData.index]['secondCommentInfo']['commentInfo'].splice([delSecondData.itemindex],1);
                commentListData['comment'][delSecondData.index]['commentCount'] -= 1;
                that.setData({
                    commentListData:commentListData
                });
                mark = 0;
            });
         };




         //上拉加载
         function pullUpLoad (){
            that.setData({
                upLoadFlag:0
            });
            modal.loading();
            var data = {
                'yp:api:article:info':{
                    'id':that['data']['commentListData']['article_id'],
                    'page':that['data']['upLoadPage'],
                    'pageSize':10
                }
            };
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function(res){
                var retData = res['self:oc:order:batch'];
                if(res['yp:api:article:info']['ret'] != 0){
                    // modal.toast(res['yp:api:article:info']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                } 
                
                var listData = res['yp:api:article:info']['data'];
                //没有更多数据了
                if(listData['comment'].length < 10){
                    that.setData({
                        upLoadFlag:0
                    });
                }else{
                    that.setData({
                        upLoadFlag:1
                    });
                }
                for(var i = 0; i < listData['comment'].length ; i++){
                    listData['comment'][i]['userInfo']['face'] = filter.filter(listData['comment'][i]['userInfo']['face']); 
                    listData['comment'][i]['content'] = filter.filter(listData['comment'][i]['content']);
                    listData['comment'][i]['createTime'] = formatTime.formatTime(listData['comment'][i]['createTime']);
                    listData['comment'][i]['likeTabFlag'] = 0;
                    var commentItem = listData['comment'][i]['secondCommentInfo'];
                    if(commentItem['commentInfo']){
                        for(var j = 0; j< commentItem['commentInfo'].length; j ++){
                            commentItem['commentInfo'][j]['content'] = filter.filter(commentItem['commentInfo'][j]['content']);
                            if(commentItem['commentInfo'][j]['isMe'] != 0){
                                commentItem['commentInfo'][j]['isDel'] = true;
                            }
                        }
                    }
                }
                var commentList = that['data']['commentListData']['comment'].concat(listData['comment']);
                that['data']['commentListData']['comment'] = commentList;
                
                that.setData({
                    commentListData:that['data']['commentListData']
                });
                modal.hide();
            });
        }

    },
    data: {
        nickName:'',
        medalMaskFlag : false
    },
    onLoad: function(options) {
        
        modal.loading();
        var that = this;
        app.getUserInfo(function (userInfo) {
            that['data']['nickName'] = userInfo['nickName']
        });

        that.setData({
            goFlag: options['flag']
        });

        var data = {
            'yp:api:article:info':{
                'id':options.id,
                'page':1,
                'pageSize':10
            }
        }

        request.post({
            path: '/wxapp_api/api'
        },data)
        .then(function (res){
            if(res['yp:api:article:info']['ret'] != 0){
                // modal.toast(res['yp:api:article:info']['msg']);
                wx.navigateTo({
                    url: "../error/error"
                })
                return false;
            }
            
            var listData = res['yp:api:article:info']['data'];
            if(listData['comment'].length < 10){
                that.setData({
                    upLoadFlag:0
                });
            }

            if(listData['comment'].length >= 0 ){
                for(var i = 0; i < listData['comment'].length ; i++){
                    listData['comment'][i]['userInfo']['face'] = filter.filter(listData['comment'][i]['userInfo']['face']); 
                    listData['comment'][i]['content'] = filter.filter(listData['comment'][i]['content']);
                    listData['comment'][i]['likeTabFlag'] = 0;
                    listData['comment'][i]['createTime'] = formatTime.formatTime(listData['comment'][i]['createTime']);
                    var commentItem = listData['comment'][i]['secondCommentInfo'];
                    if(commentItem['commentInfo']){
                        for(var j = 0; j< commentItem['commentInfo'].length; j ++){
                            commentItem['commentInfo'][j]['content'] = filter.filter(commentItem['commentInfo'][j]['content']);
                            if(commentItem['commentInfo'][j]['isMe'] != 0){
                                commentItem['commentInfo'][j]['isDel'] = true;
                            }
                        }
                    }
                }
            }
            

            that.setData({
                commentListData:listData
            });

            modal.hide();
        }); 
    },
    goArticle:function(){
        var that = this;
        var flag =that['data']['goFlag'];
        if (flag == 1){
            wx.navigateBack({
                delta: 1
            })
        }else{
            wx.redirectTo({
                url: '../article/index?id=' + that['data']['commentListData']['article_id']
            })
        }
        
    },
    onShareAppMessage: function () {
        var that = this;
        return {
            title: that['data']['nickName']+ ':' +that['data']['commentListData']['share_title'],
            path: 'pages/comment-list/comment-list?id=' + that['data']['commentListData']['article_id'] + '&flag=0'
        }
    },
    showMedalMask: function(){
        this.setData({
            medalMaskFlag : true
        });
    },
    hideMedalMask: function(){
        this.setData({
            medalMaskFlag : false
        });
    }
};

utils.assign(pageSetting,comment,goTop);
Page(pageSetting);